/*
 * IOUtils.java
 * Created on 08-Nov-2004
 * 
 * Copyright 2004 Plamen Dragozov plamen@dragozov.com
 * 
 */
package com.popcap.util;

import java.io.*;

/**
 * IOUtils
 * 
 * Description: <br/>
 * 
 * @author Plamen Dragozov
 *  
 */
public class IOUtils
{

	/**
	 * Utility method for loading a Properties object from a stream using
	 * the given character encoding.
	 * <br/>
	 * Supports multiline property values and '#' starting one-line comments.
	 * <br/>
	 * The values are trimmed so any leading new lines or spaces will be
	 * removed!
	 * 
	 * @param is
	 *            The input stream to read the properties from.
	 * @param enc
	 *            The properties character encoding.
	 * @return
	 * @throws IOException
	 */
	public static OrderedProperties readProperties(InputStream is, String enc) throws IOException
	{
		
		OrderedProperties props = new OrderedProperties();
		//read using the given encoding
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, enc));
		int index;
		String key = null, value = null;
		//read every line and add the properties found
		for(String line = reader.readLine();line != null;line = reader.readLine())
		{
			//HACK, HACK, HACK for arabic, there is a weird char 0x641 which is screwed by the preprocessor
			String weirdHack = (char)65533 + ""+(char)63;
			char araFEH = (char)0x641;
			int hackIndex = line.indexOf(weirdHack);
			while(hackIndex >= 0)
			{				
				line = line.substring(0, hackIndex) + araFEH+line.substring(hackIndex+2);
				hackIndex = line.indexOf(weirdHack);
			}
			//END of HACK
			line = line.trim();
			if(line.length() > 0 && !line.startsWith("#"))
			{
				index = line.indexOf('=');

				if(index > 0)
				{
					if(key != null)
					{
						if (props.getProperty(key) != null)
						{
							System.out.println("*** DUPLICATE PROPERTY: " + key);
						}
						props.put(key, value.trim());
						key = null;
						value = null;
					}
					key = line.substring(0, index).trim();
					if(index < (line.length() - 1))
					{
						value = line.substring(index + 1).trim();
					}
					else
					{
						value = "";
					}
				}
				else if(key != null)
				{
					//append the new line to the propertie's value
					value += "\n" + line;
				}
			}
		}
		if(key != null)
		{
			//the last line
			if (props.getProperty(key) != null)
			{
				System.out.println("*** DUPLICATE PROPERTY: " + key);
			}
			props.put(key, value.trim());
		}
		return props;
	}

	/**
	 * Write a color to an output stream using the first 3 right bytes of the
	 * given integer.
	 * 
	 * @param color
	 * @param out
	 * @throws Exception
	 */
	public static void writeColor(int color, OutputStream out) throws IOException
	{
		out.write((color >>> 16) & 0xFF);
		out.write((color >>> 8) & 0xFF);
		out.write((color >>> 0) & 0xFF);
	}

	public static void main(String[] args) throws Exception
	{
		FileInputStream fio = new FileInputStream("Z:\\ChuzzleMobile\\J2ME\\preprocessed-props\\text\\ara\\texts-ara.properties");
		OrderedProperties props = readProperties(fio, "UTF-8");
		
//		BufferedReader reader = new BufferedReader(new InputStreamReader(fio, "UTF-8"));
//		int i = 0;
//		while (i<188)
//		{
////			System.out.println(i);
//			reader.readLine();
//			i++;
//		}
		String l = props.getProperty("description.trophies");
		String weirdHack = (char)65533 + ""+(char)63;
		System.out.println("Index:" +l.indexOf(weirdHack));
		int index = l.indexOf(weirdHack);
		while(index >= 0)
		{
			
			l = l.substring(0, index) + (char)0x641+l.substring(index+2);
			index = l.indexOf(weirdHack);
		}
		for(int j = 0;j < l.length();j++ )
		{
			System.out.println((int)l.charAt(j));
		}
	}
}